//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#ifndef __ELASTOS_SCHEDULE_H__
#define __ELASTOS_SCHEDULE_H__

// BUG: testing.h ???
#ifndef TIMEPEG_DISABLE_PREEMPTION
#define TIMEPEG_DISABLE_PREEMPTION
#define TIMEPEG_RESTORE_PREEMPTION
#endif

EXTERN_C bool_t   g_bPreemption;

INLINE bool_t PreemptionEnabled()
{
    return g_bPreemption;
}

EXTERN_C void EnablePreemption();

INLINE bool_t DzDisablePreemption()
{
    bool_t bOriginalPreemption = InterlockedExchange(&g_bPreemption, FALSE);

    TIMEPEG_DISABLE_PREEMPTION

    return bOriginalPreemption;
}

INLINE void DzRestorePreemption(bool_t bPreemption)
{
    assert(!g_bPreemption);

    TIMEPEG_RESTORE_PREEMPTION

    if (bPreemption) EnablePreemption();
}

INLINE void RestorePreemptionByIsr(bool_t bPreemption)
{
    assert(!g_bPreemption);

    TIMEPEG_RESTORE_PREEMPTION

    g_bPreemption = bPreemption;
}

EXTERN void     Schedule();
EXTERN void     ScheduleNoReturn();

EXTERN void NeedReschedule();

EXTERN void CDECL ScheduleTimeout(void *pvParameter);

class ScheduleClass;

EXTERN ScheduleClass    *g_pDefaultScheduleClass;
EXTERN uint_t           g_uDefaultSchedulePolicy;

EXTERN void GetScheduleClass(
        ThreadPriorityRank priorityRank,
        ScheduleClass **ppScheduleClass, uint_t *puSchedulePolicy);

#endif // __ELASTOS_SCHEDULE_H__
